package com.lagou.logindemo.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 登录拦截器，用于登录鉴权
 */
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 拦截请求，用于登录鉴权
     *
     * @param request HTTP请求
     * @param response HTTP响应
     * @param handler 处理器
     * @return 操作结果
     * @throws Exception
     */
    public boolean preHandle(HttpServletRequest request,
                             HttpServletResponse response, Object handler) throws Exception {
        if (!"/loginSuccess.html".equals(request.getRequestURI())) {
            return true;
        }

        Boolean authResult = (Boolean) request.getSession().getAttribute("login");
        if ((authResult == null) || !authResult) {
            response.sendRedirect("/Forbiden.html");
            return false;
        }

        return true;
    }
}
